﻿using MSAcademic.v2.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MSAcademic.v2.Infrastructure.ContentSection;
using MSAcademic.v2.Infrastructure.ContentSection.Impl;

namespace MSAcademic.v2.Infrastructure.SubSection.Impl
{
    public class SubSectionQueriesImpl : ISubSectionQueries
    {

        public ISubSection Get(int id)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSection = db.SubSections.SingleOrDefault(x => x.Id == id && x.IsHidden == false);

                var subsection = new SubSectionImpl
                {
                    Id = dbSubSection.Id,
                    Name = dbSubSection.Name,
                    ContentSectionId = dbSubSection.ContentSectionId,
                    ThumbnailUrl = dbSubSection.ThumbnailUrl,
                    IsHidden = dbSubSection.IsHidden
                };

                return subsection;
            }

        }

        public ISubSection TryGetByName(string name)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSection = db.SubSections.SingleOrDefault(x => x.Name == name && !x.IsHidden);

                var subsection = new SubSectionImpl
                {
                    Id = dbSubSection.Id,
                    Name = dbSubSection.Name,
                    ContentSectionId = dbSubSection.ContentSectionId,
                    ThumbnailUrl = dbSubSection.ThumbnailUrl,
                    IsHidden = dbSubSection.IsHidden
                };

                return subsection;

            }
        }

        public IEnumerable<ISubSection> GetAllForContentSection(int contentsectionid)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var dbSubSectionList = db.SubSections
                    .Where(x => x.Id != null && x.IsHidden == false && x.ContentSectionId == contentsectionid)
                    .OrderByDescending(x => x.Name)
                    .Select(x => new SubSectionImpl
                    {
                        Id = x.Id,
                        Name = x.Name,
                        ContentSectionId = x.ContentSectionId,
                        ThumbnailUrl = x.ThumbnailUrl,
                        IsHidden = x.IsHidden
                    }).ToList();

                return dbSubSectionList;

            }
        }

        public IEnumerable<ISubSection> GetAll()
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var subsections =
                    db.SubSections.Where(x => !x.IsHidden)
                                  .Select(
                        m =>
                        new SubSectionImpl
                            {
                                ContentSectionId = m.ContentSectionId,
                                Id = m.Id,
                                IsHidden = m.IsHidden,
                                Name = m.Name,
                                ThumbnailUrl = m.ThumbnailUrl
                            }).ToList();
                return subsections;
            }
        }

        public IEnumerable<ISubSection> GetByName(string name)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                return db.SubSections.Where(x => x.Name == name && !x.IsHidden)
                                     .Select(m =>
                                             new SubSectionImpl
                                                 {
                                                     ContentSectionId = m.ContentSectionId,
                                                     Id = m.Id,
                                                     IsHidden = m.IsHidden,
                                                     Name = m.Name,
                                                     ThumbnailUrl = m.ThumbnailUrl
                                                 }).ToList();
            }
        }

        public Tuple<IContentSection, ISubSection> GetWithContentSection(int subsectionId)
        {
            using (var db = new MSAcademicDatabaseContainer())
            {
                var subSection = db.SubSections.SingleOrDefault(m => m.Id == subsectionId);
                if (subSection == null) return null;
                var parentContentSection = subSection.ContentSection;
                return new Tuple<IContentSection, ISubSection>(new ContentSectionImpl
                                                                   {
                                                                       Id = parentContentSection.Id,
                                                                       Name = parentContentSection.Name
                                                                   }, new SubSectionImpl
                                                                                      {
                                                                                          ContentSectionId = subSection.ContentSectionId,
                                                                                          Id = subSection.Id,
                                                                                          IsHidden = subSection.IsHidden,
                                                                                          Name = subSection.Name,
                                                                                          ThumbnailUrl = subSection.ThumbnailUrl
                                                                                      });

            }
        }


    }
}
